<?php

namespace Home\Model;

use Think\Model;

/**
 * Description of GameModel
 *
 * @author xtceetg
 */
class GameModel extends Model {

    protected $tableName = 'game_list';

    /**
     * 所有开服的游戏
     * @return array
     */
    public function gameAll($recommend = 0, $letterid = 0, $limit = array()) {
        $sql = "SELECT gl.* ,gg.`name` FROM game_list AS gl,game_cate AS gg";
        $where = " WHERE gg.cid = gl.cateid and gl.display=1";
        $order = " order by id desc";
        $limits = '';
        if ($recommend != 0) {
            $where.=" and recommend='" . $recommend . "'";
        }
        if ($letterid != 0) {
            $where.=" and letterid='" . $letterid . "'";
        }
        if (is_array($limit) && !empty($limit)) {
            $limits.=" limit " . $limit[0] . " , " . $limit[1];
        }
        $sql.=$where . $order . $limits;
        return $this->query($sql);
    }

    public function serverList($sid = 0, $gid = 0, $limit = array()) {
        $sql = "SELECT g.gamename,g.enname,a.area_num,a.game_id,a.areaname,a.open_time,a.fee_time,a.maintenance_start,a.maintenance_end,a.state,a.ip,a.domain,a.hf_gid,a.hf_sid,a.ishf,a.server_key FROM game_list AS g,game_server_list AS a";
        $where = " WHERE g.id = a.game_id and a.state=1";
        if ($sid != 0 && $gid != 0) {
            $where.=" and game_id='" . $gid . "' and area_num='" . $sid . "'";
        }
        if ($sid == 0 && $gid != 0) {
            $where.=" and game_id='" . $gid . "'";
        }
        $where.=" ORDER BY open_time DESC";
        if (is_array($limit) && !empty($limit)) {
            $where.=" limit " . $limit[0] . " , " . $limit[1];
        }
        $sql.=$where;
        return $this->query($sql);
    }

    /**
     * 游戏中心分页
     * @param int $start 记录开始行数
     * @param int $end  取多少条记录
     * @param int $cateid 游戏类型ID
     * @param int $subjectid 游戏题材ID
     * @param int $fightid 战斗形式ID
     * @param int $letterid 字母排序ID
     * @return array
     */
    public function Pagination($start, $end, $cateid, $subjectid, $fightid, $letterid) {
        $sql = "SELECT gl.* ,gg.`name` FROM game_list AS gl,game_cate AS gg ";
        $where = "WHERE gg.cid = gl.cateid and gl.display=1";
        if (!empty($cateid)) {
            $cateid == 1 ? : $where.=" and cateid=" . $cateid;
        }
        if (!empty($subjectid)) {
            $subjectid == 7 ? : $where.=" and subjectid=" . $subjectid;
        }
        if (!empty($fightid)) {
            $fightid == 20 ? : $where.=" and fightid=" . $fightid;
        }
        if (!empty($letterid)) {
            $letterid == 24 ? : $where.=" and letterid=" . $letterid;
        }
        $orderby = " order by createtime desc ";
        $limit = "limit " . $start . " , " . $end;
        //echo $sql . $where . $orderby . $limit;
        //exit;
        return $this->query($sql . $where . $orderby . $limit);
    }

}
